Passed
Pull Request — master (#165)
by Mathieu
01:54
created

DateUtilsAdapter.addDaysToDate   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
import { Injectable } from '@nestjs/common';
2
import {
3
  format as fnsFormat,
4
  isWeekend as fnsIsWeekend,
5
  getDaysInMonth as fnsGetDaysInMonth,
6
  eachDayOfInterval,
7
  addDays
8
} from 'date-fns';
9
import { IDateUtils } from 'src/Application/IDateUtils';
10
11
@Injectable()
12
export class DateUtilsAdapter implements IDateUtils {
13
  constructor(private readonly date: Date = new Date()) {}
14
15
  public format(date: Date, format: string): string {
16
    return fnsFormat(date, format);
17
  }
18
19
  public getDaysInMonth(date: Date): number {
20
    return fnsGetDaysInMonth(date);
21
  }
22
23
  public isWeekend(date: Date): boolean {
24
    return fnsIsWeekend(date);
25
  }
26
27
  public getCurrentDate(): Date {
28
    return this.date;
29
  }
30
31
  public getCurrentDateToISOString(): string {
32
    return this.date.toISOString();
33
  }
34
35
  public addDaysToDate(date: Date, days: number): Date {
36
    const copy = new Date(Number(date));
37
    copy.setDate(date.getDate() + days);
38
39
    return copy;
40
  }
41
42
  public getWorkedDaysDuringAPeriod(start: Date, end: Date): Date[] {
43
    const dates: Date[] = [];
44
    const workedFreeDays: Date[] = [];
45
46
    for (let year = start.getFullYear(); year <= end.getFullYear(); year++) {
47
      workedFreeDays.push(...this.getWorkedFreeDays(year));
48
    }
49
50
    for (const day of eachDayOfInterval({ start, end })) {
51
      if (
52
        this.isWeekend(day) ||
53
        workedFreeDays.filter(d => d.toISOString() === day.toISOString())
54
          .length > 0
55
      ) {
56
        continue;
57
      }
58
59
      dates.push(day);
60
    }
61
62
    return dates;
63
  }
64
65
  public getWorkedFreeDays(year: number): Date[] {
66
    const fixedDays: Date[] = [
67
      new Date(`${year}-01-01`), // New Year's Day
68
      new Date(`${year}-05-01`), // Labour Day
69
      new Date(`${year}-05-08`), // Victory in 1945
70
      new Date(`${year}-07-14`), // National Day
71
      new Date(`${year}-08-15`), // Assumption
72
      new Date(`${year}-11-01`), // All Saints' Day
73
      new Date(`${year}-11-11`), // The Armistice
74
      new Date(`${year}-12-25`) // Christmas
75
    ];
76
77
    const easterDate = this.getEasterDate(year);
78
    const easterDays: Date[] = [
79
      addDays(easterDate, 1), // Easter Monday
80
      addDays(easterDate, 39) // Ascension
81
    ];
82
83
    return [...fixedDays, ...easterDays];
84
  }
85
86
  public getEasterDate(year: number): Date {
87
    const a = year % 19;
88
    const b = Math.floor(year / 100);
89
    const c = year % 100;
90
    const d = Math.floor(b / 4);
91
    const e = b % 4;
92
    const f = Math.floor((b + 8) / 25);
93
    const g = Math.floor((b - f + 1) / 3);
94
    const h = (19 * a + b - d - g + 15) % 30;
95
    const i = Math.floor(c / 4);
96
    const k = c % 4;
97
    const l = (32 + 2 * e + 2 * i - h - k) % 7;
98
    const m = Math.floor((a + 11 * h + 22 * l) / 451);
99
    const n0 = h + l + 7 * m + 114;
100
    const n = Math.floor(n0 / 31) - 1;
101
    const p = (n0 % 31) + 1;
102
103
    return new Date(year, n, p);
104
  }
105
106
  public getLeaveDuration(startDate: string, isStartsAllDay: boolean, endDate: string, isEndsAllDay: boolean): number {
107
    let duration = this.getWorkedDaysDuringAPeriod(
108
      new Date(startDate),
109
      new Date(endDate)
110
    ).length;
111
112
    if (false === isStartsAllDay) {
113
      duration -= 0.5;
114
    }
115
116
    if (false === isEndsAllDay && duration > 0.5) {
117
      duration -= 0.5;
118
    }
119
120
    return duration;
121
  }
122
}
123